package org.itboys.shop.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.json.JsonUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.member.entity.Member;
import org.itboys.member.service.MemberService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.trade.entity.Comment;
import org.itboys.trade.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 游记
 */
@Controller
@RequestMapping(value = "/wx/comment")
public class WXCommentController extends BaseController{

	@Autowired
	private CommentService commentService;
	@Autowired 
	private MemberService memberService;
	@Autowired
	private ResourceHolder resourceHolder;
	/**
	 * 分页查询
	 * @param request
	 * @param response
	 */
	@RequestMapping("/list")
	public void list(Integer type,Long shopId, HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("shopId", shopId);
		//param.put("type", type);
		Page<Comment> pageNav = commentService.pageQuery(param,request);
		List<Long> ids = new ArrayList<Long>();
		List<Comment> list = pageNav.getData();
		for (Comment bean : list) {
			if (bean!=null) {
				ids.add(bean.getUserId());
			}
		}
		param.clear();
		param.put("id in", ids);
		List<Member> members = memberService.list(param);
		for (Member member : members) {
			for (Comment bean : list) {
				if (bean.getUserId()==member.getId()) {
					bean.setNickName(member.getNickname());
					bean.setMemberLogo(member.getLogo());
				}
			}
		}
		JsonPageUtils.renderJsonPage(pageNav.getTotal(), list, response);
	}
	/**
	 * 编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toEdit")
	public ModelAndView toEditCat(Comment bean, Model model){
		model.addAttribute("bean", bean);
		return new ModelAndView("comment/form");
	}
	
	@RequestMapping("add")
	public void add(Comment bean,HttpServletRequest request, HttpServletResponse response){
		try {
			Member user = (Member) request.getSession().getAttribute("user");
			Long memberId = user.getId();
			bean.setCreateTime(new Date());
			bean.setUserId(memberId);
			commentService.save(bean);
			AjaxUtils.renderText(response, JsonUtils.objectToString(ResultPageDOHelper.getMsgResultDO(null, "操作成功!")));
		} catch (Exception e) {
			logger.error("add error", e);
			AjaxUtils.renderText(response, JsonUtils.objectToString(ResultPageDOHelper.getErrorResultDO(-1, "系统异常!")));
		}
	}
}
